package annotation;

import org.openqa.selenium.WebElement;
import tools.AnnotationUtil;

import java.lang.annotation.*;
import java.util.function.BiPredicate;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Page {
    /**
     * 能够判断是否在该页面的元素的位置
     * @return Locator
     */
    Locator id();

    /**
     * 从页面寻找元素的等待时间，单位毫秒 ms
     * @return mill seconds
     */
    long timeout() default 3000;

    /**
     * 判断条件，默认为元素存在
     * @return Predicate<WebElement>
     */
    Class<? extends BiPredicate<WebElement,Args>> predication() default ElementExists.class;

//    String[] addtionalArgs() default {};

    class ElementExists implements BiPredicate<WebElement,Args>{
        @Override
        public boolean test(WebElement element,Args addtionalArgs) {
            return element != null;
        }
    }

    /**
     *
     * @author: Wang Yuxin
     * @email: 1325943548@qq.com
     * @create: 2022-10-04 21:17
     * @see tools.annotation.source.TextArgEntry
     */
    class TextEquals implements BiPredicate<WebElement,Args>{
        @Override
        public boolean test(WebElement element,Args addtionalArgs) {
            String text = AnnotationUtil.getArgValue(addtionalArgs,"text");
            return element != null && text!=null && element.getText().equals(text);
        }
    }
}
